/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.controller;

import java.util.Date;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.legendshop.oa.constants.FunctionEnum;
import com.legendshop.oa.service.ProjectMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.legendshop.dao.support.PageSupport;
import com.legendshop.framework.controller.BaseController;
import com.legendshop.oa.constants.DataSortResult;
import com.legendshop.oa.constants.EventTypeEnum;
import com.legendshop.oa.constants.PathResolver;
import com.legendshop.oa.dto.EventQueryDto;
import com.legendshop.oa.dto.ProjectPlanQueryDto;
import com.legendshop.oa.exception.BusinessException;
import com.legendshop.oa.model.AdminUser;
import com.legendshop.oa.model.Project;
import com.legendshop.oa.model.ProjectPlan;
import com.legendshop.oa.model.UserEvent;
import com.legendshop.oa.pages.AdminTilesPage;
import com.legendshop.oa.pages.BackPage;
import com.legendshop.oa.pages.RedirectPage;
import com.legendshop.oa.security.UserDetail;
import com.legendshop.oa.security.UserManager;
import com.legendshop.oa.service.EventService;
import com.legendshop.oa.service.ProjectPlanService;
import com.legendshop.oa.service.ProjectService;
import com.legendshop.oa.util.CommonServiceWebUtil;
import com.legendshop.oa.util.PageSupportHelper;
import com.legendshop.permission.service.AdminUserService;
import com.legendshop.util.AppUtils;

/**
 * 项目计划 控制器
 */
@Controller
@RequestMapping("/admin/projectPlan")
public class ProjectPlanController extends BaseController {

	@Autowired
	private ProjectPlanService projectPlanService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private EventService eventService;
	
	@Autowired
	private AdminUserService adminUserService;

	@Autowired
	private ProjectMemberService projectMemberService;

	/**
	 * 列表查询
	 */
	@RequestMapping("/query/{projectId}")
	public String query(HttpServletRequest request, HttpServletResponse response, @PathVariable Long projectId, ProjectPlanQueryDto projectPlanQueryDto) {
		if (AppUtils.isBlank(projectId)) {
			throw new BusinessException("Project id can not be null while loading project members");
		}
		DataSortResult sortResult = CommonServiceWebUtil.isDataSortByExternal(request, new String[] {"pm.module_id","pm.start_date", "pm.end_date"});
		projectPlanQueryDto.setProjectId(projectId);
		projectPlanQueryDto.setSortResult(sortResult);
		UserDetail userDetail = UserManager.getUser(request);
		Project project = projectService.getProject(userDetail, projectId);
		request.setAttribute("project", project);
		PageSupport<ProjectPlan> ps = projectPlanService.getProjectPlanByProject(projectPlanQueryDto);
		PageSupportHelper.savePage(request, ps);

		if(AppUtils.isNotBlank(projectPlanQueryDto.getUserName())){
			AdminUser adminUser = adminUserService.getAdminUserByUserName(projectPlanQueryDto.getUserName());
			if(adminUser != null){
				request.setAttribute("userRealName", adminUser.getRealName());
			}
		}
		request.setAttribute("projectPlanQueryDto", projectPlanQueryDto);
		return PathResolver.getPath(request, response, AdminTilesPage.PROJECT_PLAN_LIST_PAGE);
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(HttpServletRequest request, HttpServletResponse response, ProjectPlan projectPlan) {
		if (AppUtils.isBlank(projectPlan.getProjectId())) {
			throw new BusinessException("project id can not be null");
		}
		if (AppUtils.isBlank(projectPlan.getUserName())) {
			throw new BusinessException("User name can not be null");
		}


		UserDetail ud = UserManager.getUser(request);

		//检查是否在项目里
		boolean isInProject = projectMemberService.isMemberInProject(ud.getUsername(), projectPlan.getProjectId());
		if(!ud.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA) && !isInProject){
			//不在項目里不能填写工时
			return "fail";// 失败
		}

		projectPlan.setCreateTime(new Date());
		projectPlan.setDeleted(0);// 默认是有效的
		UserDetail userDetail = UserManager.getUser(request);
		projectPlan.setUserDetail(userDetail);
		projectPlanService.saveProjectPlan(projectPlan);
		return "success";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete/{id}")
	public String delete(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) {
		ProjectPlan projectPlan = projectPlanService.getProjectPlan(id);
		if (projectPlan != null) {
			UserDetail ud = UserManager.getUser(request);
			//检查是否在项目里
			boolean isInProject = projectMemberService.isMemberInProject(ud.getUsername(), projectPlan.getProjectId());
			if(!ud.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA) && !isInProject){
				//不在項目里不能填写工时
				String actionUrl = RedirectPage.PROJECT_PLAN_LIST_QUERY.getNativeValue() + "/" + projectPlan.getProjectId();
				return PathResolver.getPath(request, response, actionUrl, RedirectPage.VARIABLE);
			}


			projectPlanService.deleteProjectPlan(projectPlan);
			saveMessage(request, ResourceBundle.getBundle("i18n/ApplicationResources").getString("entity.deleted"));
			String actionUrl = RedirectPage.PROJECT_PLAN_LIST_QUERY.getNativeValue() + "/" + projectPlan.getProjectId();
			return PathResolver.getPath(request, response, actionUrl, RedirectPage.VARIABLE);
		}
		return null;
	}

	/**
	 * 根据Id加载
	 */
	@RequestMapping(value = "/loadByProject/{projectId}")
	public String loadByProject(HttpServletRequest request, HttpServletResponse response,
			@PathVariable Long projectId) {
		Project project = projectService.getProjectWithModule(projectId);
		request.setAttribute("project", project);
		request.setAttribute("projectId", projectId);
		return PathResolver.getPath(request, response, BackPage.PROJECT_PLAN_EDIT_PAGE);
	}

	/**
	 * 加载编辑页面
	 */
	@RequestMapping(value = "/load/{projectPlanId}")
	public String load(HttpServletRequest request, HttpServletResponse response, @PathVariable Long projectPlanId) {
		ProjectPlan projectPlan = projectPlanService.getProjectPlan(projectPlanId);
		request.setAttribute("projectPlan", projectPlan);
		request.setAttribute("projectId", projectPlan.getProjectId());
		
		Project project = projectService.getProjectWithModule(projectPlan.getProjectId());
		request.setAttribute("project", project);

		EventQueryDto eventQueryDto = new EventQueryDto();
		eventQueryDto.setRelateId(projectPlanId);
		eventQueryDto.setType(EventTypeEnum.PLAN.value());
		eventQueryDto.setPageSize(20);
		PageSupport<UserEvent> ps = eventService.getEventPage(eventQueryDto);
		PageSupportHelper.savePage(request, ps);
		return PathResolver.getPath(request, response, BackPage.PROJECT_PLAN_EDIT_PAGE);
	}

}
